﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using Touch2Like.Model;
using System.Data;

namespace Touch2Like.DAL
{
    public class CouponRedeemDAL
    {
        #region Add
        public int Add(CouponRedeemInfo info)
        {
            Database db = new Database();
            SqlParameter[] param =
            {
                db.MakeOutParam("@ListID",SqlDbType.Int,4),
                db.MakeInParam("@UserGUID",SqlDbType.UniqueIdentifier,16,new Guid(info.UserGUID)),
                db.MakeInParam("@DesignID",SqlDbType.Int,4,info.DesignID),
                db.MakeInParam("@BrowserGUID",SqlDbType.UniqueIdentifier,16,new Guid(info.BroswerGUID)),
                db.MakeInParam("@Email",SqlDbType.VarChar,50,info.Email),
                db.MakeInParam("@ReviewID",SqlDbType.Int,4,info.ReviewID),
                db.MakeInParam("@CouponCode",SqlDbType.VarChar,50,info.CouponCode),
                db.MakeInParam("@IsRedeemed",SqlDbType.Int,4,info.IsRedeemed),
                db.MakeInParam("@CouponCodeLocation",SqlDbType.VarChar,200,info.CouponCodeLocation),
                db.MakeInParam("@RedeemDate",SqlDbType.DateTime,8,info.RedeemDate)
            };
            try
            {
                db.ExecNonQuery("T2R_CouponRedeem_Add", param);
                return DataHelper.GetInt(param[0].Value).Value;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                db.Close();
            }
        }
        #endregion

        #region Update
        public void Update(CouponRedeemInfo info)
        {
            Database db = new Database();
            SqlParameter[] param =
            {
                db.MakeInParam("@ListID",SqlDbType.Int,4,info.ListID),
                db.MakeInParam("@DesignID",SqlDbType.Int,4,info.DesignID),
                db.MakeInParam("@BrowserGUID",SqlDbType.UniqueIdentifier,16,new Guid(info.BroswerGUID)),
                db.MakeInParam("@Email",SqlDbType.VarChar,50,info.Email),
                db.MakeInParam("@ReviewID",SqlDbType.Int,4,info.ReviewID),
                db.MakeInParam("@CouponCode",SqlDbType.VarChar,50,info.CouponCode),
                db.MakeInParam("@IsRedeemed",SqlDbType.Int,4,info.IsRedeemed),
                db.MakeInParam("@CouponCodeLocation",SqlDbType.VarChar,200,info.CouponCodeLocation),
                db.MakeInParam("@RedeemDate",SqlDbType.DateTime,8,info.RedeemDate)
            };
            try
            {
                db.ExecNonQuery("T2R_CouponRedeem_Update", param);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                db.Close();
            }
        }
        #endregion

        #region Get By ID
        public CouponRedeemInfo GetByID(int listid)
        {
            Database db = new Database();
            SqlDataReader dr = null;
            SqlParameter[] param =
            {
                db.MakeInParam("@ListID",SqlDbType.Int,4,listid)
            };
            try
            {
                dr = db.RunProc("T2R_CouponRedeem_GetByID", param);
                if (dr.Read())
                {
                    CouponRedeemInfo info = new CouponRedeemInfo();
                    info.ListID = DataHelper.GetInt(dr["ListID"]);
                    info.UserGUID = DataHelper.GetString(dr["UserGUID"]);
                    info.DesignID = DataHelper.GetInt(dr["DesignID"]);
                    info.BroswerGUID = DataHelper.GetString(dr["BroswerGUID"]);
                    info.Email = DataHelper.GetString(dr["Email"]);
                    info.ReviewID = DataHelper.GetInt(dr["ReviewID"]);
                    info.CouponCode = DataHelper.GetString(dr["CouponCode"]);
                    info.IsRedeemed = DataHelper.GetInt(dr["IsRedeemed"]);
                    info.CouponCodeLocation = DataHelper.GetString(dr["CouponCodeLocation"]);
                    info.CreateDate = DataHelper.GetDateTime(dr["CreateDate"]);
                    info.RedeemDate = DataHelper.GetDateTime(dr["RedeemDate"]);
                    return info;
                }
                else
                {
                    return null;
                }

            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                dr.Close();
                db.Close();
            }
        }
        #endregion

        #region Get By Coupon Code
        public CouponRedeemInfo GetByCouponCode(string code)
        {
            Database db = new Database();
            SqlDataReader dr = null;
            SqlParameter[] param =
            {
                db.MakeInParam("@CouponCode",SqlDbType.VarChar,50,code)
            };
            try
            {
                dr = db.RunProc("T2R_CouponRedeem_GetByCouponCode", param);
                if (dr.Read())
                {
                    CouponRedeemInfo info = new CouponRedeemInfo();
                    info.ListID = DataHelper.GetInt(dr["ListID"]);
                    info.UserGUID = DataHelper.GetString(dr["UserGUID"]);
                    info.DesignID = DataHelper.GetInt(dr["DesignID"]);
                    info.BroswerGUID = DataHelper.GetString(dr["BroswerGUID"]);
                    info.Email = DataHelper.GetString(dr["Email"]);
                    info.ReviewID = DataHelper.GetInt(dr["ReviewID"]);
                    info.CouponCode = DataHelper.GetString(dr["CouponCode"]);
                    info.IsRedeemed = DataHelper.GetInt(dr["IsRedeemed"]);
                    info.CouponCodeLocation = DataHelper.GetString(dr["CouponCodeLocation"]);
                    info.CreateDate = DataHelper.GetDateTime(dr["CreateDate"]);
                    info.RedeemDate = DataHelper.GetDateTime(dr["RedeemDate"]);
                    return info;
                }
                else
                {
                    return null;
                }

            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                dr.Close();
                db.Close();
            }
        }
        #endregion

        public DataTable GetByUserGUID(string userguid)
        {
            Database db = new Database();

            try
            {
                SqlParameter[] param =
                {
                    db.MakeInParam("@UserGUID", SqlDbType.UniqueIdentifier, 16, new Guid(userguid))
                };
                return db.GetDataSet("T2R_CouponRedeem_GetByUserGUID", param).Tables[0];
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                db.Close();
            }
        }
    }
}
